package com.lagou.rpcprovider2;


import com.lagou.rpcprovider2.service.UserServiceImpl;
import com.lagou.service.util.ZookeeperUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Server2Boot {

    public static String ip = "127.0.0.1";
    public static int port = 9002;

    public static void main(String[] args) throws Exception {
        //启动服务器
        UserServiceImpl.startServer(ip, port);
        //同时将ip及端口号注册到zookeeper
        registerZookeeper(ip, port);
        SpringApplication.run(Server2Boot.class, args);
    }

    /**
     * 将服务端的IP和端口注册到zookeeper
     */
    private static void registerZookeeper(String ip, int port) throws Exception {
        //1.建立连接
        CuratorFramework client = ZookeeperUtil.createSession("192.168.174.128:2181");
        //2.创建临时节点
        String level1Path = "/registerCenter";
        String level2Path = "/" + ip + ":" + port;
        String path = level1Path + level2Path;

        String s = client.create().creatingParentsIfNeeded()
                .withMode(CreateMode.EPHEMERAL).forPath(path);

        System.out.println("节点递归创建成功，该节点路径" + s);

    }
}
